/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sst.Modelo;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author alberto
 */
public class ConexionDB {
    Connection con;
    PreparedStatement ps;
    java.sql.Statement stmt = null;

    public ConexionDB(){
    try {
            Class.forName("com.mysql.jdbc.Driver");
            String URL = "jdbc:mysql://localhost/sst";
            con = (Connection) DriverManager.getConnection(URL, "root", "alberto.1990");
        } catch (SQLException ex) {
           JOptionPane.showMessageDialog(null, "error","eroor",JOptionPane.ERROR_MESSAGE);
        } catch (ClassNotFoundException ex) {
            System.out.print(ex);
        }

   }

    public void conectar (){
    try {
            Class.forName("com.mysql.jdbc.Driver");
            String URL = "jdbc:mysql://localhost/sst";
            con = (Connection) DriverManager.getConnection(URL, "root", "alberto.1990");
            System.out.print("conectado");
        } catch (SQLException ex) {
           JOptionPane.showMessageDialog(null, "error","eroor",JOptionPane.ERROR_MESSAGE);
        } catch (ClassNotFoundException ex) {
            System.out.print(ex);
        }
    }

    public boolean validarUsuario(String usuario, String clave) throws SQLException {
        boolean logico = false;
        String user = usuario;
        String pass = clave;
        stmt = con.createStatement();
        ResultSet resultado = (ResultSet) stmt.executeQuery("SELECT usuario,pass FROM user WHERE usuario ='" +user+"'AND pass ='"+pass+"'");
        try {
            if (resultado.next()) {
                logico = true;
            } else {
                logico=false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return logico;
    }

   

    public boolean listaUsuarios(){
    String user = null;
       // Acceso acceso = new Acceso();
        boolean a = true;
    //String a = null;
        try {
            java.sql.Statement listaUser = con.createStatement();
            java.sql.ResultSet resultado = listaUser.executeQuery("SELECT usuario FROM user");
            while (resultado.next()){
                
               user = resultado.getString("usuario");
                
                
            }
            a=false;
        } catch (SQLException ex) {
            Logger.getLogger(ConexionDB.class.getName()).log(Level.SEVERE, null, ex);
        }

    return a;
    }

    public String listaCliente(){
    String cliente = "";
       // Acceso acceso = new Acceso();
        boolean a = true;
    //String a = null;
        try {
            java.sql.Statement listaCliente = con.createStatement();
            java.sql.ResultSet resultado = listaCliente.executeQuery("SELECT nombreCliente FROM cliente");
            while (resultado.next()){

               cliente = resultado.getString("nombreCliente");
            }
            a=false;
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Error","Problemas en DB",JOptionPane.ERROR_MESSAGE);
        }

    return cliente;
    }

    public void cerrarConexion()
	 {
		if(con!=null)
			try {
				con.setAutoCommit(true);
				con.close() ;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		con=null;
	}

}
    


